/******************************************************************

 SWAZAM - MySWAzam Javascript

 ******************************************************************/

///////////////////////////////////////////////////////////////////
// CONSTANTS
// ensure consistency with the templates and the server urls
///////////////////////////////////////////////////////////////////

/**
 * Used to store information about HTML ids and constants for the templates
 * @enum {string}
 */
var templateConstants = {
	//template names
	homeTemplate:"#mySWAzamTemplate",
	trackTemplate:"#trackTemplate",
	profileTemplate:"#profileTemplate",
	//basic elements on the mySWAzam page
	profileButton:"#profileButton",
	logoutButton:"#logoutButton",
	trackStreamTarget:"#trackStream",
	//profile template
	profileModalDialogID:"#profileModal",
	profileModalDialogDataArea:"#profileData"
};

/**
 * Stores information about server side access paths
 * @enum {string}
 */
var serverURLs = {
	logoutURL:"service/web/logout",
	profileURL:"service/web/profile",
	trackStreamURL:"service/web/tracks"
};

///////////////////////////////////////////////////////////////////
// THE ACTUAL CLASS
///////////////////////////////////////////////////////////////////

/**
 * @constructor
 */
function MySWAzam(data) {
	// model stuff
	this.username = data.username;
	this.firstname = data.firstname;
	this.lastname = data.lastname;
	// internal variables
	/**
	 * precompiled template id
	 * @const
	 * @type {string}
	 * @private
	 */
	this.trackTemplateCompiled = "compiledTrackTemplate";
}

/**
 * Init method for MySWAzam
 *
 * @param {string} targetElement specifies where to render the MySWAzam components
 */
MySWAzam.prototype.init = function (targetElement) {
	//create instance for anonymous functions
	var instance = this;
	//display the mySWAzam page
	$(targetElement).html($(templateConstants.homeTemplate).tmpl(this));
	//register the button handlers
	$(templateConstants.logoutButton).click(function () {
		instance.logout();
	});
	$(templateConstants.profileButton).click(function () {
		instance.showProfile();
	});
	//compile track stream template
	$.template(this.trackTemplateCompiled, $(templateConstants.trackTemplate));
	//show track stream
	this.fetchTracks();
	//create interval for track stream	
	this.trackStreamInterval = setInterval(function () {
		instance.fetchTracks();
	}, 3000);
}

/**
 * Fetches the trackstream and invokes updateTrackStream to display the newly read tracks
 */
MySWAzam.prototype.fetchTracks = function () {
	$.ajax({
		context:this,
		dataType:"json",
		url:serverURLs.trackStreamURL,
		success:this.updateTrackStream
	});
}

/**
 * Updates the track stream template
 * @private
 */
MySWAzam.prototype.updateTrackStream = function (data) {
	$(templateConstants.trackStreamTarget).html($.tmpl(this.trackTemplateCompiled, {"tracks":data}));
}

/**
 * Shows the profile dialog
 */
MySWAzam.prototype.showProfile = function () {
	$.ajax({
		dataType:"json",
		url:serverURLs.profileURL,
		success:function (data) {
			$(templateConstants.profileModalDialogID).modal("show");
			$(templateConstants.profileModalDialogDataArea).html($(templateConstants.profileTemplate).tmpl(data));
		}
	});
}

/**
 * Performs logout
 */
MySWAzam.prototype.logout = function () {
	$.post(serverURLs.logoutURL).done(function () {
		clearInterval(this.trackStreamInterval);
		location.reload(true); //perform this kind of redirect to help the browser cleaning up
	});
}